package com.lemon.rule.express.instruction.op;

import java.lang.reflect.Array;
import java.util.List;

import com.lemon.rule.express.Operator;

public class OperatorIn extends Operator {
    public OperatorIn(String aName) {
        this.name = aName;
    }

    public OperatorIn(String aAliasName, String aName, String aErrorInfo) {
        this.name = aName;
        this.aliasName = aAliasName;
        this.errorInfo = aErrorInfo;
    }

    public Object executeInner(Object[] list) throws Exception {
        Object obj = list[0];
        if (obj == null) {
            // ����Ϊ�գ�����ִ�з���
            String msg = "����Ϊ�գ�����ִ�з���:";
            throw new Exception(msg + this.name);
        } else if (((obj instanceof Number) || (obj instanceof String)) == false) {
            String msg = "�������Ͳ�ƥ�䣬ֻ�����ֺ��ַ������ͲŲ���ִ�� in ����,��ǰ����������:";
            throw new Exception(msg + obj.getClass().getName());
        } else if (list.length == 2 && (list[1].getClass().isArray() || list[1] instanceof List)) {
            if (obj.equals(list[1]) == true) {
                return true;
            } else if (list[1].getClass().isArray()) {
                int len = Array.getLength(list[1]);
                for (int i = 0; i < len; i++) {
                    boolean f = OperatorEqualsLessMore.executeInner("==", obj, Array.get(list[1], i));
                    if (f == true) {
                        return Boolean.TRUE;
                    }
                }
            } else if (list[1] instanceof List) {
                @SuppressWarnings("unchecked")
                List<Object> array = (List<Object>) list[1];
                for (int i = 0; i < array.size(); i++) {
                    boolean f = OperatorEqualsLessMore.executeInner("==", obj, array.get(i));
                    if (f == true) {
                        return Boolean.TRUE;
                    }
                }
            }
            return false;
        } else {
            for (int i = 1; i < list.length; i++) {
                boolean f = OperatorEqualsLessMore.executeInner("==", obj, list[i]);
                if (f == true) {
                    return Boolean.TRUE;
                }
            }
            return Boolean.FALSE;
        }
    }

}